<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="考试名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入考试名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="导入日期" prop="createTime">
        <el-date-picker clearable
                        v-model="queryParams.createTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择导入日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <!--      <el-col :span="1.5">
              <el-button
                type="primary"
                plain
                icon="el-icon-plus"
                size="mini"
                @click="handleAdd"
                v-hasPermi="['system:manage:add']"
              >新增</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button
                type="success"
                plain
                icon="el-icon-edit"
                size="mini"
                :disabled="single"
                @click="handleUpdate"
                v-hasPermi="['system:manage:edit']"
              >修改</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button
                type="danger"
                plain
                icon="el-icon-delete"
                size="mini"
                :disabled="multiple"
                @click="handleDelete"
                v-hasPermi="['system:manage:remove']"
              >删除</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button
                type="warning"
                plain
                icon="el-icon-download"
                size="mini"
                @click="handleExport"
                v-hasPermi="['system:manage:export']"
              >导出</el-button>
            </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-download"
          size="mini"
          @click="importTemplate"
          v-hasPermi="['system:manage:importTemplate']"
        >下载模版
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
          v-hasPermi="['system:manage:import']"
        >导入模版
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="manageList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <!--      <el-table-column label="id" align="center" prop="id" />-->
      <el-table-column
        label="序号"
        type="index"
        width="50">
      </el-table-column>
      <el-table-column label="导入时间" align="center" prop="createTime" width="150"/>
      <!--        <template slot-scope="scope">-->
      <!--          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>-->
      <!--        </template>-->
      <el-table-column label="考试名称" align="center" prop="name"/>
      <el-table-column
        align="center"
        fixed="right"
        label="操作"
        width="210">>
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:manage:edit']"
          >修改
          </el-button>
          <!--                    <el-button
                                size="mini"
                                type="text"
                                icon="el-icon-delete"
                                @click="handleDelete(scope.row)"
                                v-hasPermi="['system:manage:remove']"
                              >删除
                                      </el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleShowData(scope.row)"
            v-hasPermi="['system:manage:query']"
          >数据查看
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-pie-chart"
            @click="handleHistogram(scope.row)"
            v-hasPermi="['system:manage:edit']"
          >柱状图
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 柱状图弹窗 -->
    <el-dialog :title="titleHistogram" :visible.sync="openHistogram" width="50%" append-to-body>
      <template>
        <div>
          <div ref="chart" id="mainHistogram" style="height: 500px;width: 100%;"></div>
        </div>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="exportExcel">导出excel</el-button>
          <el-button @click="cancelHistogram">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 添加或修改考试管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="考试名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入内容" maxlength="30" show-word-limit/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 用户导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;"
                   @click="importTemplate">下载模板
          </el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 数据查看 -->
    <el-dialog :title="titleTable" :visible.sync="openTable" width="70%" append-to-body>
      <el-table v-loading="loadingAchievement" :data="achievementList"
                @selection-change="handleSelectionChangeAchievement">
        <!--        <el-table-column type="selection" width="55" align="center"/>-->
        <!--      <el-table-column label="id" align="center" prop="id" />-->
        <el-table-column
          fixed="left"
          label="序号"
          type="index"
          width="50">
        </el-table-column>
        <el-table-column label="考试名称" align="center" prop="examName" width="250px" fixed="left"/>
        <el-table-column label="学员姓名" align="center" prop="studentName" width="75" fixed="left"/>
        <el-table-column label="科目一分数" align="center" prop="subjectOne" width="85"/>
        <el-table-column label="科目一速度" align="center" prop="subjectOneSpeed" width="85"/>
        <el-table-column label="科目一评定" align="center" prop="subjectOneEvaluate" width="85"/>
        <el-table-column label="科目二分数" align="center" prop="subjectTwo" width="85"/>
        <el-table-column label="科目二速度" align="center" prop="subjectTwoSpeed" width="85"/>
        <el-table-column label="科目二评定" align="center" prop="subjectTwoEvaluate" width="85"/>
        <el-table-column label="科目三分数" align="center" prop="subjectThree" width="85"/>
        <el-table-column label="科目三速度" align="center" prop="subjectThreeSpeed" width="85"/>
        <el-table-column label="科目三评定" align="center" prop="subjectThreeEvaluate" width="85"/>
        <el-table-column label="科目四分数" align="center" prop="subjectFour" width="85"/>
        <el-table-column label="科目四速度" align="center" prop="subjectFourSpeed" width="85"/>
        <el-table-column label="科目四评定" align="center" prop="subjectFourEvaluate" width="85"/>
        <el-table-column label="科目五分数" align="center" prop="subjectFive" width="85"/>
        <el-table-column label="科目五速度" align="center" prop="subjectFiveSpeed" width="85"/>
        <el-table-column label="科目五评定" align="center" prop="subjectFiveEvaluate" width="85"/>
        <el-table-column label="科目六分数" align="center" prop="subjectSix" width="85"/>
        <el-table-column label="科目六速度" align="center" prop="subjectSixSpeed" width="85"/>
        <el-table-column label="科目六评定" align="center" prop="subjectSixEvaluate" width="85"/>
        <el-table-column label="科目七分数" align="center" prop="subjectSeven" width="85"/>
        <el-table-column label="科目七速度" align="center" prop="subjectSevenSpeed" width="85"/>
        <el-table-column label="科目七评定" align="center" prop="subjectSevenEvaluate" width="85"/>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="60">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdateAchievement(scope.row)"
              v-hasPermi="['system:achievement:edit']"
            >修改
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="totalAchievement>0"
        :total="totalAchievement"
        :page.sync="queryParamsAchievement.pageNum"
        :limit.sync="queryParamsAchievement.pageSize"
        @pagination="getListAchievement"
      />

      <div slot="footer" class="dialog-footer">
        <!--        <el-button type="primary" @click="handleSelectAchievement">确 定</el-button>-->
        <el-button @click="cancelTable">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改学员考试成绩管理对话框 -->
    <el-dialog :title="titleAchievementUpdate" :visible.sync="openAchievementUpdate" width="1200px" append-to-body>
      <el-form ref="formAchievement" :model="formAchievement" :rules="rulesAchievement" label-width="100px">
        <!--        <el-form-item label="考试id" prop="examId">
                  <el-input v-model="formAchievement.examId" placeholder="请输入考试id"/>
                </el-form-item>
                <el-form-item label="学员id" prop="studentId">
                  <el-input v-model="formAchievement.studentId" placeholder="请输入学员id"/>
                </el-form-item>-->

        <el-row>
          <el-col :span="12">
            <el-form-item label="考试名称" prop="examName">
              <el-input v-model="formAchievement.examName" placeholder="请输入考试名称" :disabled="true"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="学员姓名" prop="studentName">
              <el-input v-model="formAchievement.studentName" placeholder="请输入学员姓名" :disabled="true"/>
            </el-form-item>
          </el-col>

        </el-row>


        <el-row>
          <el-col :span="8">
            <el-form-item label="科目一分数" prop="subjectOne">
              <el-input v-model="formAchievement.subjectOne" placeholder="请输入科目一分数"
                        oninput="value= value.match(/\d+(\.\d{0,2})?/) ? value.match(/\d+(\.\d{0,2})?/)[0] : ''"
                        maxlength="6" show-word-limit clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="科目一速度" prop="subjectOneSpeed">
              <el-input v-model="formAchievement.subjectOneSpeed" placeholder="请输入科目一速度"
                        oninput="value= value.match(/\d+(\.\d{0,2})?/) ? value.match(/\d+(\.\d{0,2})?/)[0] : ''"
                        maxlength="6" show-word-limit clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="科目一评定" prop="subjectOneEvaluate">
              <el-select
                v-model="formAchievement.subjectOneEvaluate"
                placeholder="科目一评定"
                clearable
              >
                <el-option
                  v-for="dict in dict.type.subject_evaluate"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.label"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="科目二分数" prop="subjectTwo">
              <el-input v-model="formAchievement.subjectTwo" placeholder="请输入科目二分数"
                        oninput="value= value.match(/\d+(\.\d{0,2})?/) ? value.match(/\d+(\.\d{0,2})?/)[0] : ''"
                        maxlength="6" show-word-limit clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="科目二速度" prop="subjectTwoSpeed">
              <el-input v-model="formAchievement.subjectTwoSpeed" placeholder="请输入科目二速度"
                        oninput="value= value.match(/\d+(\.\d{0,2})?/) ? value.match(/\d+(\.\d{0,2})?/)[0] : ''"
                        maxlength="6" show-word-limit clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="科目二评定" prop="subjectTwoEvaluate">
              <el-select
                v-model="formAchievement.subjectTwoEvaluate"
                placeholder="科目二评定"
                clearable
              >
                <el-option
                  v-for="dict in dict.type.subject_evaluate"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.label"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="科目三分数" prop="subjectThree">
              <el-input v-model="formAchievement.subjectThree" placeholder="请输入科目三分数"
                        oninput="value= value.match(/\d+(\.\d{0,2})?/) ? value.match(/\d+(\.\d{0,2})?/)[0] : ''"
                        maxlength="6" show-word-limit clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="科目三速度" prop="subjectThreeSpeed">
              <el-input v-model="formAchievement.subjectThreeSpeed" placeholder="请输入科目三速度"
                        oninput="value= value.match(/\d+(\.\d{0,2})?/) ? value.match(/\d+(\.\d{0,2})?/)[0] : ''"
                        maxlength="6" show-word-limit clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="科目三评定" prop="subjectThreeEvaluate">
              <el-select
                v-model="formAchievement.subjectThreeEvaluate"
                placeholder="科目三评定"
                clearable
              >
                <el-option
                  v-for="dict in dict.type.subject_evaluate"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.label"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="科目四分数" prop="subjectFour">
              <el-input v-model="formAchievement.subjectFour" placeholder="请输入科目四分数"
                        oninput="value= value.match(/\d+(\.\d{0,2})?/) ? value.match(/\d+(\.\d{0,2})?/)[0] : ''"
                        maxlength="6" show-word-limit clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="科目四速度" prop="subjectFourSpeed">
              <el-input v-model="formAchievement.subjectFourSpeed" placeholder="请输入科目四速度"
                        oninput="value= value.match(/\d+(\.\d{0,2})?/) ? value.match(/\d+(\.\d{0,2})?/)[0] : ''"
                        maxlength="6" show-word-limit clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="科目四评定" prop="subjectFourEvaluate">
              <el-select
                v-model="formAchievement.subjectFourEvaluate"
                placeholder="科目四评定"
                clearable
              >
                <el-option
                  v-for="dict in dict.type.subject_evaluate"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.label"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="科目五分数" prop="subjectFive">
              <el-input v-model="formAchievement.subjectFive" placeholder="请输入科目五分数"
                        oninput="value= value.match(/\d+(\.\d{0,2})?/) ? value.match(/\d+(\.\d{0,2})?/)[0] : ''"
                        maxlength="6" show-word-limit clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="科目五速度" prop="subjectFiveSpeed">
              <el-input v-model="formAchievement.subjectFiveSpeed" placeholder="请输入科目五速度"
                        oninput="value= value.match(/\d+(\.\d{0,2})?/) ? value.match(/\d+(\.\d{0,2})?/)[0] : ''"
                        maxlength="6" show-word-limit clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="科目五评定" prop="subjectFiveEvaluate">
              <el-select
                v-model="formAchievement.subjectFiveEvaluate"
                placeholder="科目五评定"
                clearable
              >
                <el-option
                  v-for="dict in dict.type.subject_evaluate"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.label"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>


        <el-row>
          <el-col :span="8">
            <el-form-item label="科目六分数" prop="subjectSix">
              <el-input v-model="formAchievement.subjectSix" placeholder="请输入科目六分数"
                        oninput="value= value.match(/\d+(\.\d{0,2})?/) ? value.match(/\d+(\.\d{0,2})?/)[0] : ''"
                        maxlength="6" show-word-limit clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="科目六速度" prop="subjectSixSpeed">
              <el-input v-model="formAchievement.subjectSixSpeed" placeholder="请输入科目六速度"
                        oninput="value= value.match(/\d+(\.\d{0,2})?/) ? value.match(/\d+(\.\d{0,2})?/)[0] : ''"
                        maxlength="6" show-word-limit clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="科目六评定" prop="subjectSixEvaluate">
              <el-select
                v-model="formAchievement.subjectSixEvaluate"
                placeholder="科目六评定"
                clearable
              >
                <el-option
                  v-for="dict in dict.type.subject_evaluate"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.label"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>


        <el-row>
          <el-col :span="8">
            <el-form-item label="科目七分数" prop="subjectSeven">
              <el-input v-model="formAchievement.subjectSeven" placeholder="请输入科目七分数"
                        oninput="value= value.match(/\d+(\.\d{0,2})?/) ? value.match(/\d+(\.\d{0,2})?/)[0] : ''"
                        maxlength="6" show-word-limit clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="科目七速度" prop="subjectSevenSpeed">
              <el-input v-model="formAchievement.subjectSevenSpeed" placeholder="请输入科目七速度"
                        oninput="value= value.match(/\d+(\.\d{0,2})?/) ? value.match(/\d+(\.\d{0,2})?/)[0] : ''"
                        maxlength="6" show-word-limit clearable/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="科目七评定" prop="subjectSevenEvaluate">
              <el-select
                v-model="formAchievement.subjectSevenEvaluate"
                placeholder="科目七评定"
                clearable
              >
                <el-option
                  v-for="dict in dict.type.subject_evaluate"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.label"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormAchievement">确 定</el-button>
        <el-button @click="cancelAchievement">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {listManage, getManage, delManage, addManage, updateManage, barGraph} from "@/api/system/manage";
import {getToken} from "@/utils/auth";
import {url} from "@/utils/url";
import * as echarts from 'echarts';
import ExcelJS from 'exceljs';    // 引入exceljs, 用于生成excel文件
import {saveAs} from 'file-saver'
import {getAchievement, listAchievement, updateAchievement} from "@/api/system/achievement";
import {addStudent} from "@/api/system/student"; // 引入file-saver, 用于保存文件

export default {
  name: "Manage",
  dicts: ['subject_evaluate'],
  mounted() {

  },
  data() {
    return {

      chart: null,
      // 遮罩层
      loading: true,

      loadingAchievement: true,
      // 选中数组
      ids: [],
      achievementIds: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      totalAchievement: 0,
      // 考试管理表格数据
      manageList: [],
      // 学员考试成绩管理表格数据
      achievementList: [],
      // 弹出层标题
      title: "",
      titleTable: "",
      titleAchievementUpdate: "",
      // 是否显示弹出层
      open: false,
      openTable: false,
      openAchievementUpdate: false,
      // 柱状图
      openHistogram: false,

      // 柱状图标题
      titleHistogram: "",
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: {Authorization: "Bearer " + getToken()},
        // 上传的地址
        url: url + "/system/manage/importData"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        createTime: null,
      },
      queryParamsAchievement: {
        pageNum: 1,
        pageSize: 10,
        examId: null,
        examName: null,
        name: null,
        studentId: null,
        studentName: null,
        createTime: null,
        createDate: [],
      },
      // 表单参数
      form: {},
      // 表单参数
      formAchievement: {},
      // 表单校验
      rules: {
        name: [
          {required: true, message: "考试名称不能为空", trigger: "blur"}
        ]
      },
      rulesAchievement: {
        examId: [
          {required: true, message: "考试id不能为空", trigger: "blur"}
        ],
        studentId: [
          {required: true, message: "学员id不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      listManage(this.queryParams).then(response => {
        this.manageList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancelHistogram() {
      this.openHistogram = false
      this.resetQuery();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消按钮
    cancelTable() {
      this.openTable = false;
      this.resetQuery();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加考试管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getManage(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改考试管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateManage(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addManage(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除考试管理编号为"' + ids + '"的数据项？').then(function () {
        return delManage(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/manage/export', {
        ...this.queryParams
      }, `manage_${new Date().getTime()}.xlsx`)
    },
    /** 柱状图按钮操作 */
    myCharts(row) {

      barGraph(row.id).then(response => {

        const excellentList = response.data.excellentList;
        const goodList = response.data.goodList;
        const passList = response.data.passList;
        const failList = response.data.failList;

        // 检测是否已经存在echarts实例，如果不存在，则不再去初始化
        let myChart = echarts.getInstanceByDom(document.getElementById('mainHistogram'))
        // 如果为空 则正常进行渲染 反之 不再进行初始化
        if (myChart == null) {
          const dom = document.getElementById('mainHistogram');
          myChart = echarts.init(dom, null, {
            renderer: 'canvas',
            useDirtyRect: false
          });
        }

        const app = {};
        let option;

        const posList = [
          'left',
          'right',
          'top',
          'bottom',
          'inside',
          'insideTop',
          'insideLeft',
          'insideRight',
          'insideBottom',
          'insideTopLeft',
          'insideTopRight',
          'insideBottomLeft',
          'insideBottomRight'
        ];
        app.configParameters = {
          rotate: {
            min: -90,
            max: 90
          },
          align: {
            options: {
              left: 'left',
              center: 'center',
              right: 'right'
            }
          },
          verticalAlign: {
            options: {
              top: 'top',
              middle: 'middle',
              bottom: 'bottom'
            }
          },
          position: {
            options: posList.reduce(function (map, pos) {
              map[pos] = pos;
              return map;
            }, {})
          },
          distance: {
            min: 0,
            max: 100
          }
        };
        app.config = {
          rotate: 90,
          align: 'left',
          verticalAlign: 'middle',
          position: 'insideBottom',
          distance: 15,
          onChange: function () {
            const labelOption = {
              rotate: app.config.rotate,
              align: app.config.align,
              verticalAlign: app.config.verticalAlign,
              position: app.config.position,
              distance: app.config.distance
            };
            myChart.setOption({
              series: [
                {
                  label: labelOption
                },
                {
                  label: labelOption
                },
                {
                  label: labelOption
                },
                {
                  label: labelOption
                }
              ]
            });
          }
        };
        const labelOption = {
          show: true,
          position: app.config.position,
          distance: app.config.distance,
          align: app.config.align,
          verticalAlign: app.config.verticalAlign,
          rotate: app.config.rotate,
          formatter: '{c}  {name|{a}}',
          fontSize: 16,
          rich: {
            name: {}
          }
        };
        option = {
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'shadow'
            }
          },
          legend: {
            data: ['优秀人数', '良好人数', '及格人数', '不及格人数']
          },
          // toolbox: {
          //   show: true,
          //   orient: 'vertical',
          //   left: 'right',
          //   top: 'center',
          //   feature: {
          //     mark: {show: true},
          //     dataView: {show: true, readOnly: false},
          //     magicType: {show: true, type: ['line', 'bar', 'stack']},
          //     restore: {show: true},
          //     saveAsImage: {show: true}
          //   }
          // },
          xAxis: [
            {
              type: 'category',
              axisTick: {show: false},
              data: ['科目一', '科目二', '科目三', '科目四', '科目五', '科目六', '科目七']
            }
          ],
          yAxis: [
            {
              type: 'value'
            }
          ],
          series: [
            {
              name: '优秀人数',
              type: 'bar',
              barGap: 0,
              label: labelOption,
              emphasis: {
                focus: 'series'
              },
              data: excellentList
            },
            {
              name: '良好人数',
              type: 'bar',
              label: labelOption,
              emphasis: {
                focus: 'series'
              },
              data: goodList
            },
            {
              name: '及格人数',
              type: 'bar',
              label: labelOption,
              emphasis: {
                focus: 'series'
              },
              data: passList
            },
            {
              name: '不及格人数',
              type: 'bar',
              barGap: 0,
              label: labelOption,
              emphasis: {
                focus: 'series'
              },
              data: failList
            }
          ]
        };

        myChart.setOption(option);
        // 根据页面大小自动响应图表大小
        // window.addEventListener("resize", function () {
        //   myChart.resize();
        // });
      });
    },

    exportExcel() {
      const workbook = new ExcelJS.Workbook(); // 创建工作簿
      const worksheet = workbook.addWorksheet('考试管理柱状图可视化分析'); // 添加工作表

      const chart = echarts.getInstanceByDom(this.$refs.chart) // 获取图表实例
      const base64Image = chart.getDataURL({
        pixelRatio: 2, // 导出图片的分辨率比例，默认为1，即图片的分辨率为屏幕分辨率的一倍
        backgroundColor: '#fff' // 导出图片的背景色
      })
      let image = workbook.addImage({ // 添加图片
        base64: base64Image, // 图片的base64编码
        extension: 'png'  // 图片的扩展名
      });
      worksheet.addImage(image, 'A1:Q34'); // 将图片添加到工作表中
      workbook.xlsx.writeBuffer().then(function (buffer) { // 生成excel文件的二进制数据
        saveAs.saveAs(new Blob([buffer], {  // 生成Blob对象
          type: 'application/octet-stream'  // 指定文件的MIME类型
        }), '考试管理柱状图可视化分析.xlsx');  // 指定文件名
      });
    },


    /** 柱状图按钮操作 */
    handleHistogram(row) {
      this.openHistogram = true;
      this.titleHistogram = row.name;
      this.$nextTick(() => {
        this.myCharts(row)
      })
    },

    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "考试管理导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('system/manage/importTemplate', {}, `考试管理导入模版.xlsx`)
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", {dangerouslyUseHTMLString: true});
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },

    /** 数据查看按钮操作 */
    handleShowData(row) {
      this.openTable = true;
      this.titleTable = row.name + "数据查看";
      this.getListAchievement(row)
    },

    // 多选框选中数据
    handleSelectionChangeAchievement(selection) {
      this.achievementIds = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 查询学员成绩列表 */
    getListAchievement(row) {
      //this.loadingAchievement = false;
      this.queryParamsAchievement.examId = row.id;
      listAchievement(this.queryParamsAchievement).then(response => {
        this.achievementList = response.rows;
        this.totalAchievement = response.total;
        this.loadingAchievement = false;
      }).catch(() => {
        this.loadingAchievement = false;
      });
    },
    // 取消按钮
    cancelAchievement() {
      this.openAchievementUpdate = false;
      this.reset();
    },

    /** 修改按钮操作 */
    handleUpdateAchievement(row) {
      this.resetAchievement();
      const id = row.id || this.achievementIds
      getAchievement(id).then(response => {
        this.formAchievement = response.data;
        this.openAchievementUpdate = true;
        this.titleAchievementUpdate = "修改学员成绩";
      });
    },

    /** 提交按钮 */
    submitFormAchievement() {
      this.$refs["formAchievement"].validate(valid => {
        if (valid) {
          if (this.formAchievement.id != null) {
            updateAchievement(this.formAchievement).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.openAchievementUpdate = false;

              //刷新数据
              this.queryParamsAchievement.examId = this.formAchievement.examId;
              listAchievement(this.queryParamsAchievement).then(response => {

                this.achievementList = response.rows;
                this.totalAchievement = response.total;
                this.loadingAchievement = false;
              }).catch(() => {
                this.loadingAchievement = false;
              });
            });
          }
        }
      });
    },
    // 表单重置
    resetAchievement() {
      this.formAchievement = {
        id: null,
        examId: null,
        examName: null,
        studentId: null,
        studentName: null,
        subjectOne: null,
        subjectOneSpeed: null,
        subjectOneEvaluate: null,
        subjectTwo: null,
        subjectTwoSpeed: null,
        subjectTwoEvaluate: null,
        subjectThree: null,
        subjectThreeSpeed: null,
        subjectThreeEvaluate: null,
        subjectFour: null,
        subjectFourSpeed: null,
        subjectFourEvaluate: null,
        subjectFive: null,
        subjectFiveSpeed: null,
        subjectFiveEvaluate: null,
        subjectSix: null,
        subjectSixSpeed: null,
        subjectSixEvaluate: null,
        subjectSeven: null,
        subjectSevenSpeed: null,
        subjectSevenEvaluate: null,
        createBy: null,
        createTime: null,
        createDate: null,
        updateBy: null,
        updateTime: null,
        remark: null,
      };
      this.resetForm("formAchievement");
    },
  }
}
;
</script>
